package com.sise.aio和bio和nio.nio.nio的buffer学习;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author idea
 * @data 2019/1/20
 */
public class Test2 {

    public static void main(String[] args) throws IOException {
        System.out.println("分散读取");
        RandomAccessFile randomAccessFile=new RandomAccessFile("art.txt","rw");
        FileChannel fileChannel=randomAccessFile.getChannel();

        ByteBuffer byteBuffer=ByteBuffer.allocate(100);
        ByteBuffer byteBuffer2=ByteBuffer.allocate(1024);

        //定义一个缓冲数组
        ByteBuffer[] byteBuffers={byteBuffer,byteBuffer2};

        fileChannel.read(byteBuffers);

        for (ByteBuffer buffer : byteBuffers) {
            buffer.flip();
        }

        System.out.println(new String(byteBuffers[0].array(),0,byteBuffers[0].limit()));
        System.out.println("-----------");
        System.out.println(new String(byteBuffers[1].array(),0,byteBuffers[1].limit()));


        System.out.println("聚集写入------");
        //文件的写入
        RandomAccessFile randomAccessFile1=new RandomAccessFile("test.txt","rw");
        FileChannel channel=randomAccessFile1.getChannel();
        channel.write(byteBuffers);
        channel.close();
    }
}
